from odoo import fields, models


class AgriModelVersion(models.Model):
    _name = 'agri.model.version'
    _description = 'AI 模型版本'
    _order = 'is_default desc, release_date desc, id desc'

    name = fields.Char(string='显示名称', required=True)
    model_name = fields.Char(string='模型名称', required=True)
    version_code = fields.Char(string='版本号', required=True)
    platform = fields.Selection(
        selection=[
            ('android', 'Android'),
            ('ios', 'iOS'),
            ('server', '服务器'),
            ('generic', '通用'),
        ],
        string='平台',
        default='android',
        required=True,
    )
    quantization = fields.Selection(
        selection=[
            ('fp32', 'FP32'),
            ('fp16', 'FP16'),
            ('int8', 'INT8'),
            ('dynamic', '动态量化'),
        ],
        string='量化',
        default='int8',
    )
    file_url = fields.Char(string='下载链接', required=True)
    file_checksum = fields.Char(string='SHA256 校验值')
    file_size = fields.Integer(string='文件大小 (KB)')
    release_date = fields.Date(string='发布日期', default=fields.Date.today)
    min_app_version = fields.Char(string='最低 App 版本')
    accuracy = fields.Float(string='验证准确率 (%)')
    is_default = fields.Boolean(string='默认版本', default=False)
    notes = fields.Text(string='发布说明')
    active = fields.Boolean(default=True)

    _sql_constraints = [
        ('model_platform_version_unique', 'unique(model_name, platform, version_code)',
         '同一模型同一平台的版本号必须唯一。'),
    ]

    def action_set_default(self):
        for record in self:
            domain = [('model_name', '=', record.model_name), ('platform', '=', record.platform)]
            self.search(domain).write({'is_default': False})
            record.is_default = True
        return True




